package com.jwm.power.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
@RequestMapping("/getMessage")
public class MessagePushController {
    public String getAccessToken(){
        String appId = "wxb157430b96c39666";
        String appSecret = "5a9255e5ff0c4a37afea3b09c9194cdc";
        String result = cn.hutool.http.HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject.getStr("access_token");
    }
    @RequestMapping("/pushMessage")
    public String pushMessage(){
        JSONObject body=new JSONObject();
        body.set("touser","用户的openId");
        body.set("template_id","o-jlV7ksJ0JxmMjiv-Wja5yYyXD7bwjr9lSvJqKp26Q");
        JSONObject json=new JSONObject();
        json.set("thing6",new JSONObject().set("value","20200820757539"));
        json.set("date4",new JSONObject().set("value", LocalDateTime.now()));
        json.set("amount3",new JSONObject().set("value","多功能等一件商品"));
        json.set("character_string2",new JSONObject().set("value", DateUtil.now()));
        json.set("thing5",new JSONObject().set("value","拼团"));
        body.set("data",json);
        //发送
        String accessToken= getAccessToken();
        String post =  cn.hutool.http.HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, body.toString());
        return "ok";
    }
}
